/**
 * flashsocket链接
 * by blue
 * 功能：flash方式socket接口
 */
fangimjs.define("sdk/socket", ["jquery", "util/util", "setting/setting", "event/event"], function (require, exports, module) {
    "use strict";
    /*flash加载判断操作*/
    var $ = require("jquery"),
        event = require("event/event"),
        util = require("util/util");
    var CMD_JS_INIT = "CMD_JS_INIT",
        CMD_JS_ICON_CLICKED = "CMD_JS_ICON_CLICKED",
        CMD_JS_PAGE_ACTIVE = "CMD_JS_PAGE_ACTIVE",
        CMD_JS_SEND_MES = "CMD_JS_SEND_MES",
        CMD_JS_LOGOUT = "CMD_JS_LOGOUT",
        CMD_JS_RE_CONNECT="CMD_JS_RE_CONNECT";
    var win = window;
    return {
        /**
         * 初始化函数
         */
        init: function () {
            win.FangChat = win.FangChat || {};
            /**
             * 当有新消息时
             * 收到消息socket调用
             * @param data
             */
            win.FangChat.recvMsg = function (data) {
                data = data[0];
                event.trigger('socket:msg:new', data);
                // util.trace("flash call js::recvMsg", data);
            };
            /**
             * 当IM UI改变时
             * 用户当前窗口不接收来自flash UI变化，或者接收后不处理
             * 只用非当前窗口才接收自flash UI变化
             * @param data
             */
            win.FangChat.uiDataChange = function (data) {
                data = data[0];
                event.trigger('socket:ui:change', data);
                // util.trace("flash call js:uiDataChange", data);
            };
            /**
             * 当IM 联系人列表改变时
             * 用户当前窗口不接收来自flash联系人列表变化，或者接收后不处理
             * 只用非当前窗口才接收自flash联系人列表变化
             * @param data
             */
            win.FangChat.contactListDataChange = function (data) {
                data = data[0];
                event.trigger('socket:contactlist:change', data);
            };
            /**
             * 当主client消息改变时,通知从client更新消息
             * @param data
             */
            win.FangChat.msgDataChange = function (data) {
                var obj = {};
                data = data[0];
                for (var key in data) {
                    if (data.hasOwnProperty(key)) {
                        var k = key;
                        if (k.substr(0, 13) === '_flash_toxml_' ){
                            k = k.substr(13);
                        }
                        obj[k.replace('_____', ':')] = data[key];
                    }
                }
                event.trigger('socket:msg:change', obj);
                util.trace("flash call js:contactDataChange", obj);
            };
            /**
             * 消息显示
             * @param data
             */
            win.FangChat.addMsgString = function (data) {
                data = data[0];
                event.trigger('socket:msgstring:show', data);
            };
            /**
             * 登陆成功操作
             * @param data
             */
            win.FangChat.loginSuccess = function (data) {
                data = data[0];
                util.setCookie('fangchatcookie', data, 30);
            };
            /**
             * 设置用户自己的数据
             * @param data
             */
            win.FangChat.setSelfData = function (data) {
                event.trigger('socket:selfdata:set', data[0]);
            };
            /**
             * 系统消息显示
             * @param data
             */
            win.FangChat.sysInfo = function (data) {
                data = data[0];
                event.trigger('socket:systemInfo:show', data);
            };
            /**
             * yf: flash 调用方法：获取用户 sfut cookie
             */
            win.FangChat.getSfut = function() {
                return util.getCookie('sfut');
            };
            /**
             * 登出 socket
             */
            win.FangChat.logout = function() {
                event.trigger('socket:userstate:logout');
            };

            /**
             * yangfan: 记录日志
             */
            win.FangChat.sendLog = function (data) {
                var param = {
                    c: 'tools',
                    a: 'imLog',
                    type: data[0],
                    mesg: data[1]
                    // type: data.type,
                    // message: data.uid + ',' + data.city + ',' + data.msg + ',' + (data.content || '')
                };

                $.ajax({
                    url: 'http://ditu.fang.com/index.php',
                    method: 'GET',
                    data: param
                }).done(function (data) {
                    util.trace(data.errmsg || '日志记录成功');
                }).fail(function (data) {
                    util.trace(data.errmsg || '日志记录失败');
                });
            };
        },
        /**
         * 登陆操作
         */
        login:function(){
            try {
                this.core().callIM(CMD_JS_RE_CONNECT);
            } catch (e) {
                util.trace('core().callIM CMD_JS_RE_CONNECT wrong')
            }
        },
        /**
         * 发送消息
         * @param data
         * @example {sendto:'fl:jinshengABC', content:'聊天内容'}
         */
        sendMsg: function (data) {
            if (!data.sendto) {
                util.trace('sendMsg::data.sendto in null');
                return;
            }
            var obj = data;
            // yf: 发送 location 字段给 flash
            obj.location = win.location.href;
            /*{
             command: data.command, //文字消息是chat
             content: data.content, //输入的聊天内容
             sendto: data.sendto //发送对象
             }*/
            if (data.command == 'img') {
                obj["msgContent"] = "100,100";
            }
            try {
                this.core().callIM(CMD_JS_SEND_MES, obj);
            } catch (e) {
                util.trace('core().callIM CMD_JS_SEND_MES wrong')
            }
        },
        /**
         * 发送图片
         * @param data
         * @example {sendto:'fl:jinshengABC', content:'图片地址'}
         */
        sendImage: function (data) {
            if (!data.sendto) {
                util.trace('sendImage::data.sendto in null');
                return;
            }
            var obj = {
                command: "img", //文字消息是chat
                mesContent: data.content, //输入的聊天内容
                sendto: data.sendto, //发送对象
                imgWidth: 100,
                imgHeight: 100
            };
            // yf: 发送 location 字段给 flash
            obj.location = win.location.href;
            try {
                this.core().callIM(CMD_JS_SEND_MES, obj);
            } catch (e) {
                util.trace('core().callIM CMD_JS_SEND_MES wrong')
            }

        },
        /**
         * 设置UI，以便通知其他IM窗口
         * @param data
         */
        setUiData: function (data) {
            try {
                this.core().callIM('SET_UI_DATA', data);
            } catch (e) {
                util.trace('core().callIM SET_UI_DATA wrong')
            }
        },
        /**
         * 当排联系人列表序发生的时候，需要把排序后列表传flash
         * @param data
         */
        setContactListData: function (data) {
            try {
                this.core().callIM('SET_CONTACT_LIST_DATA', data);
            } catch (e) {
                util.trace('core().callIM SET_CONTACT_LIST_DATA wrong')
            }
        },
        /**
         * 当从页面点击添加联系时调用
         * @param data
         */
        addContact: function (data) {
            try {
                !data.username && (data.username = data.contactId);
                this.core().callIM(CMD_JS_ICON_CLICKED, data);
            } catch (e) {
                util.trace('core().callIM SET_CONTACT_DATA wrong')
            }
        },
        /**
         * 设置当前活动的窗口
         * @param data
         */
        setPageActive: function (data) {
            try {
                this.core().callIM(CMD_JS_PAGE_ACTIVE, data);
            } catch (e) {
                util.trace('core().callIM SET_CONTACT_DATA wrong')
            }
        },
        /**
         * 初始flash配置信息
         */
        initSocketData: function (usertype) {
            var data = fangimjs.data.vars.config;
            var that = this;
            var config = {
                cookie: util.getCookie('fangchatcookie') || '',
                userid: data.userid||'',
                username: data.username||'',
                showname: data.showname||data.username||'',
                city: data.city || '北京',
                usertype: usertype
            };
            try {
                that.core().callIM(CMD_JS_INIT, config);
            } catch (e) {
                util.trace('core().callIM CMD_JS_INIT wrong')
            }
        },
        logout: function () {
            try {
                this.core().callIM(CMD_JS_LOGOUT);
            } catch (e) {
                util.trace('core().callIM CMD_JS_LOGOUT wrong');
            }
        }
    };
});
